package net;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class docghi2 extends Thread {
	private ObjectOutputStream out;
	private Queues<msg> Sendqueue;
	private Socket socket;
	
public docghi2(ObjectOutputStream out, Queues<msg> Sendqueue,Socket socket)
{
super();
this.out=out;
this.Sendqueue=Sendqueue;
this.socket=socket;

}
public void run()
{
	while(true)
	{
		if(!Sendqueue.isEmpty())
		{ 
			msg ms1=Sendqueue.dequeue();
			String str;
			try {
				str = socket.getInetAddress().getLocalHost().getHostAddress();//day la ip cua client 
				
				if(!ms1.getStr().equals("client"))
				{
					System.out.println("to chay phan khong phai client");
					if(str.equals(ms1.getIP()))//chi cho nhung thang nao co dung IP cua server dua ra moi duoc ghi(sending)
					{
					
					try {
						out.writeObject(ms1);
						out.flush();
						out.reset();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			
					}
					else if(!str.equals(ms1.getIP()))
					{
						
						Sendqueue.enqueue(ms1);
						
					}
					
				}
				else if(ms1.getStr().equals("client"))
				{
					System.out.println("toi chay phan client");
					try {
						out.writeObject(ms1);
						out.flush();
						out.reset();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			
					
				}
				
				
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}

	try {
		Thread.sleep(200);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	
	
	}
}
}
